package com.example.model.dict.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.*;
import com.example.db.model.IPage;
import com.example.model.dict.model.DictiModel;
import com.example.model.dict.service.DictiModelService;
import com.example.utils.Results;
import com.example.utils.StringUtils;

import java.util.Date;

/**
 * @Classname DictiModelController
 * @Description TODO
 * @Date 2022/8/2 21:06
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController(value = "/dicti",desc = "字典模块")
public class DictiModelController {

    @Autowired
    DictiModelService dictiModelService;

    @PostMapping("/parentPage")
    public JSONObject parentPageController(@Param(value = "pageNum",desc = "页码",defalultValue = "1")Integer pageNum,
                                           @Param(value = "pageSize",desc = "条数",defalultValue = "10")Integer pageSize,
                                           @Param(value = "searchParams",required = false)JSONObject object){
        IPage<DictiModel> dictiModelIPage=dictiModelService.parentPage(pageNum,pageSize,object);
        return Results.successLayuiTable(dictiModelIPage);
    }

    @PostMapping("/childPage")
    public JSONObject childPageController(@Param(value = "pageNum",desc = "页码",defalultValue = "1")Integer pageNum,
                                          @Param(value = "pageSize",desc = "条数",defalultValue = "10")Integer pageSize,
                                          @Param(value = "pid",desc = "字典父id")Integer pid,
                                          @Param(value = "searchParams",required = false)JSONObject object){
        IPage<DictiModel> dictiModelIPage=dictiModelService.childPage(pageNum,pageSize,pid,object);
        return Results.successLayuiTable(dictiModelIPage);
    }

    @PostMapping("/save")
    public JSONObject saveDictiModelController(@RequestBody DictiModel dictiModel){
        if(dictiModel == null){
            return Results.error("信息不得为空");
        }
        if(dictiModel.getPid() != null){
            DictiModel dicti=dictiModelService.getById(dictiModel.getPid());
            if(dicti==null){
                return Results.error("字典不存在");
            }
            //不让设置字典标识
            dictiModel.setDictino(dicti.getDictino());
            dictiModel.setDictname(null);
        }
        if(dictiModel.getPid()==null){
            if(StringUtils.isEmpty(dictiModel.getDictino())){
                return Results.error("字典标识不得为空");
            }
            DictiModel model=dictiModelService.getDictiModelByDictiNo(dictiModel.getDictino());
            if(model != null){
                return Results.error("字典标识已存在");
            }
            dictiModel.setPid(0);
        }
        dictiModel.setCreatedate(new Date());
        dictiModelService.save(dictiModel);
        return Results.success("保存成功");
    }

    @PostMapping("/update")
    public JSONObject updateDictiModelController(@RequestBody DictiModel dictiModel){
        if (dictiModel == null) {
            return Results.error("信息不得为空");
        }
        DictiModel dicti=dictiModelService.getById(dictiModel.getId());
        if(dicti == null){
            return Results.error("信息不存在");
        }
        //紧张修改字典标识
        dictiModel.setDictino(null);
        dictiModelService.update(dictiModel);
        return Results.success("更新成功");
    }

    @GetMapping("/getone")
    public JSONObject getOneController(@Param("id")String id){
        DictiModel dictiModel=dictiModelService.getById(id);
        if (dictiModel == null) {
            return Results.error("信息不存在");
        }
        return Results.success(dictiModel);
    }
}
